// pages/test-book/test-book.js
var util = require('../../utils/util.js')
const DB = wx.cloud.database()
const $ = DB.command.aggregate
const _ = DB.command
//获取应用实例
const app = getApp()


let skip = 20

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //处理导 航 栏的高度
    rpxdata: wx.getStorageSync('rpxdata'),
    navigationBarAndStatusBarHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 'px',
    navigationBarAndStatusBarAndsearchHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 60 + 'px',
    //设置导航栏的显示信息
    navbarData: {
      showCapsule: 1,
      title: "借出图书"
    },

  DB_jy:{},
  bottom_info: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  jieyuedate: function(startime) {
    //日期格式化
    var start_date = new Date(startime.replace(/-/g, "/"));
    var end_date = new Date();
    //转成毫秒数，两个日期相减
    var ms = end_date.getTime() - start_date.getTime();
    //转换成天数
    var day = parseInt(Math.ceil(ms / (1000 * 60 * 60 * 24)));
    //do something
    return day    
  } ,

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that =this
    let dbtmp = []
    DB.collection("lchbook").orderBy('jytime','asc').where({jystate :true}).skip(0).limit(20).get({success:(res)=>{    
      for (let i=0;i<res.data.length;i++){ 
        var obj = {}
        obj.bookname = res.data[i].bookname
        obj.jyren = res.data[i].jyren
        obj.jydate = res.data[i].jydate
        obj.isbn = res.data[i].isbn
        obj.day = that.jieyuedate(res.data[i].jytime)
        obj._id = res.data[i]._id
        dbtmp.push(obj)
  /*  const DB_tmp = new Array(res.data.length).fill({})     
      function temp(j){    
       DB_tmp[j].jyren = res.data[j].jyren 
       DB_tmp[j].bookname = res.data[j].bookname
       DB_tmp[j].jydate = res.data[j].jydate
       DB_tmp[j].isbn = res.data[j].isbn
       DB_tmp[j].day = that.jieyuedate(res.data[j].jytime) } 
       temp(i)   */
 
   /*   $.each(res.data,function(i,value){
        DB_tmp[i].jyren = value.jyren 
       DB_tmp[i].bookname = value.bookname
       DB_tmp[i].jydate = value.jydate
       DB_tmp[i].isbn = value.isbn
       DB_tmp[i].day = that.jieyuedate(value.jytime)
      })   */

      }
      that.setData({DB_jy:dbtmp})
      
    }  })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    let dbtmp = []
    let vvv = this.data.DB_jy
  DB.collection("lchbook").orderBy('jytime','asc').where({jystate :true}).skip(skip).limit(20).get({success:(res)=>{
    for (let i=0;i<res.data.length;i++){ 
      var obj = {}
      obj.bookname = res.data[i].bookname
      obj.jyren = res.data[i].jyren
      obj.jydate = res.data[i].jydate
      obj.isbn = res.data[i].isbn
      obj.day = that.jieyuedate(res.data[i].jytime)
      obj._id = res.data[i]._id
      dbtmp.push(obj)
    }
    vvv = that.data.DB_jy.concat(dbtmp)
    if (res.data.length == 0) {that.setData({bottom_info: 1 })}
    else { that.setData({DB_jy:vvv})   
    skip = skip + 20
  }
  }
  })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  

})